#!/bin/bash

function cctl()
{
    #local color=$1
    #local other=$2
    case $1 in
        r) printf "\033[$231m";; # frontcolor red
        g) printf "\033[$232m";; # frontcolor green
        y) printf "\033[$233m";; # frontcolor yellow
        b) printf "\033[$234m";; # frontcolor blue
        p) printf "\033[$235m";; # frontcolor pink
        c) printf "\033[$236m";; # frontcolor cyan
        d) printf "\033[$237m";; # frontcolor drak white = grey
        w) printf "\033[$238m";; # frontcolor white
        0) printf "\033[$20m";;  # frontcolor reset
        bgr) printf "\033[$231m";; # background_color red
        bgg) printf "\033[$232m";; # background_color green
        bgy) printf "\033[$233m";; # background_color yellow
        bgb) printf "\033[$234m";; # background_color blue
        bgp) printf "\033[$235m";; # background_color pink
        bgc) printf "\033[$236m";; # background_color cyan
        bgd) printf "\033[$237m";; # background_color drak white = grey
        bgw) printf "\033[$238m";; # background_color white
        bg0) printf "\033[$20m";;  # background_color reset
        fg*)
            local r=$(printf "%d" "0x${1:2:2}")
            local g=$(printf "%d" "0x${1:4:2}")
            local b=$(printf "%d" "0x${1:6:2}")
            printf "\033[$238;2;$r;$g;${b}m"
            ;;
        bg*)
            local r=$(printf "%d" "0x${1:2:2}")
            local g=$(printf "%d" "0x${1:4:2}")
            local b=$(printf "%d" "0x${1:6:2}")
            printf "\033[$248;2;$r;$g;${b}m"
            ;;
        *)
            if [ ${#1} = 6 ]; then
                local r=$(printf "%d" "0x${1:0:2}")
                local g=$(printf "%d" "0x${1:2:2}")
                local b=$(printf "%d" "0x${1:4:2}")
                printf "\033[$238;2;$r;$g;${b}m"
            fi
            ;;
    esac
}

function logd()
{
    local fmt="$1"; shift
    printf "${BASH_SOURCE[1]}:${BASH_LINENO[0]} $(cctl c)$fmt$(cctl 0)\n" $*
}

function logi()
{
    local fmt="$1"; shift
    printf "${BASH_SOURCE[1]}:${BASH_LINENO[0]} $(cctl g)$fmt$(cctl 0)\n" $*
}

function logw()
{
    local fmt="$1"; shift
    printf "${BASH_SOURCE[1]}:${BASH_LINENO[0]} $(cctl y)$fmt$(cctl 0)\n" $*
}

function loge()
{
    local fmt="$1"; shift
    printf "${BASH_SOURCE[1]}:${BASH_LINENO[0]} $(cctl r)$fmt$(cctl 0)\n" $*
}

declare flog_filename="$HOME/log/shell/default.log"
function set_flog_file()
{
    flog_filename="$1"
}

function flog()
{
    $* | tee -a $flog_filename
}

function flogd()
{
    local fmt="$1"; shift
    printf "$(date +%Y.%m.%d\ %H:%M:%S) ${BASH_SOURCE[1]}:${BASH_LINENO[0]} $(cctl c)$fmt$(cctl 0)\n" $* | tee -a $flog_filename
}

function flogi()
{
    local fmt="$1"; shift
    printf "$(date +%Y.%m.%d\ %H:%M:%S) ${BASH_SOURCE[1]}:${BASH_LINENO[0]} $(cctl g)$fmt$(cctl 0)\n" $* | tee -a $flog_filename
}

function flogw()
{
    local fmt="$1"; shift
    printf "$(date +%Y.%m.%d\ %H:%M:%S) ${BASH_SOURCE[1]}:${BASH_LINENO[0]} $(cctl y)$fmt$(cctl 0)\n" $* | tee -a $flog_filename
}

function floge()
{
    local fmt="$1"; shift
    printf "$(date +%Y.%m.%d\ %H:%M:%S) ${BASH_SOURCE[1]}:${BASH_LINENO[0]} $(cctl r)$fmt$(cctl 0)\n" $* | tee -a $flog_filename
}
